﻿<?php
//[php] 利用 curl 來抓取網頁結果
	include ('func_curl.php');//functuin 區
	//驗證帳號、密碼
//	$Name   = 'demo';
	$Name   = $_POST['user_id'];
//	$password   = 'open888';
	$password   = $_POST['password'];
	
	//設定header區的用戶代理
	$UA = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1';
	
/*
$url = 設定截取網址, 
$post_data = 傳遞的值, 
$headers = HTTP標頭字段的設置
*/
	$headers = array (
	$UA,
	'Content-type: application/json'
	);	
	$host = "http://120.96.183.123:5000";
	$path = "/v2.0/tokens";
	$url = $host . $path;
//	$post_data = "csrfmiddlewaretoken=0KCaquwtT2OMnX9ewxoFKzUfYy8q5uMd&region=http://127.0.0.1:5000/v2.0&id_username=$Name&id_password=$password";
	$post_data = '{"auth":{"passwordCredentials":{"username": "' . $Name . '", "password":"' . $password . '"}, "tenantId":"93e7ee28523a4b5c8b133e28d33771ae"}}'; 
	
	$response = post_https($url, $post_data,$headers);
	debug("\n pos is " . strpos($response,"\r\n\r\n") . "\n");
	$json = json_decode(substr($response,strpos($response,"\r\n\r\n")+4),true);
//	var_dump($json);
	if(!array_key_exists("access",$json))
	  $auth =($json);
	else
	  $auth = ($json["access"]["user"]);
//	$auth = $json[0];
//	debug($response);
//	$auth = json_decode($json,true);
//echo json_decode($response, true);
/*
foreach ($json as $item)
{
    foreach ($item->assets->RAM as $asset)
    {
        echo $asset->model;
    }
}
*/
	//RequestSplite方法 = HTTP Request 取的個別屬性的值
/*
	$url = $host . RequestSplite($response,'Location');
	$cookie = RequestSplite($response,'Set-Cookie');
	$headers = array (
	$UA,
	"Cookie: $cookie",
	"Cache-control: max-age=3600"
	);	
	debug($url . "\n"); 
	debug($cookie . "\n");
//	var_dump($headers);

	$response = get_https($url,$headers);
	debug($response);
	if( $response !=NULL)
	{
		$url = RequestSplite($response,'Location');
		$cookie2 = RequestSplite($response,'Set-Cookie');
		$headers = array (
		$UA,
		"Cookie: $cookie; $cookie2",
		"Cache-control: no-cache"
		);	
		$response = get_https($url,$headers);
		$auth='success';
		//var_dump($response);
		debug($response);
		debug('\n</pre>\n');
		
	}
	//else
		 //echo "<script>alert ('Error!'); window.history.go(-1);</script>";
*/
?>
